<template>
  <section class="tag-container">
    <!-- 过滤条件 -->
    <div class="tag-filter">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="标签名">
          <el-input v-model="tagName" placeholder="请输入标签名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getTagData">查询</el-button>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-button type="primary" @click="addTagShow = true">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <el-table
      :data="tagData"
      style="width: 100%">
      <el-table-column
        prop="_id"
        label="ID"
        align="center"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="name"
        label="标签名"
        align="center"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.createTime | FDateTime}}
        </template>
      </el-table-column>
      <el-table-column
        prop="num"
        label="文章数量"
        align="center"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="danger" @click="delTag(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加标签弹窗 -->
    <el-dialog
      title="添加标签"
      :visible.sync="addTagShow"
      width="30%"
      center
      append-to-body>
      <el-form :model="addForm" :rules="rules" ref="addForm">
        <el-form-item label="标签名" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入标签名" clearable></el-input>
        </el-form-item>

        <el-form style="text-align: center;">
          <el-button @click="addTagShow = false; addForm.name = ''">取 消</el-button>
          <el-button type="primary" @click="addTag">确 定</el-button>
        </el-form>
      </el-form>
    </el-dialog>
  </section>
</template>

<script>
export default {
  layout: 'admin',
  data () {
    return {
      addTagShow: false,
      tagName: '',
      addForm: {
        name: ''
      },
      rules: {
        name: [{required: true, message: '请输入标签名', trigger: 'blur' }]
      },
      tagData: []
    }
  },
  methods: {
    // 获取标签
    getTagData () {
      this.$http.get('/tag/get', {
        params: {
          name: this.tagName
        }
      }).then(res => {
        this.tagData = res.data
      })
    },
    // 添加标签
    addTag () {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.$http.post('/tag/add', this.addForm).then(res => {
            if (res.code === 0) {
              this.$message.success(res.msg)
              this.addTagShow = false
              this.addForm.name = ''
              this.getTagData()
            }
          })
        } else {
          return false;
        }
      });
    },
    // 删除标签
    delTag (el) {
      this.$confirm(`删除标签：${el.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.get('/tag/del', {
          params: {
            id: el._id
          }
        }).then(res => {
          if (res.code === 0) {
            this.$message.success(res.msg)
            this.getTagData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  },
  created () {
    this.getTagData()
  }
}
</script>

<style lang="scss">
  .tag-container {
    .tag-filter {
      text-align: center;
    }
  }
</style>
